package com.johnny.xia.tread;

import com.johnny.xia.util.cache.RedisUtil;

public class RedisTask extends Thread {

    private String key = null;
    private Long num = 0L;
    private String threadName=null;

    private Long total = 0L;
    private RedisUtil ru = new RedisUtil();

    public RedisTask(String threadName,String key,Long num){
        this.key = key;
        this.num = num;
        this.threadName = threadName;
    }

    @Override
    public void run() {
        while(true) {

            Long currentNum = ru.getNum(key);
//            if(currentNum <= 0){
//                System.out.println(threadName+" 共领取2：" + total);
//                return;
//            }
            Long value = ru.decrBy(key, num);
//            System.out.println(threadName+" 剩余-->" + value);


            if(value < 0){
                ru.incrBy(key,num);
                System.out.println(threadName+" 剩余-->" + value +",不够领取："+num);
                System.out.println(threadName+" 共领取：" + total);
                ru.closeServer();
                return;
            }else{
                total += num;
            }

        }
    }
}
